const db = wx.cloud.database()
var app = getApp();
import Toast from '@vant/weapp/toast/toast'
// miniprogram/pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false
  },

  getUserInfo: function(e) {
    if (this.data.passwd_check != this.data.passwd) {
      Toast.fail('签到码错误!!!!');
    }
    else{
      wx.cloud.callFunction({
        name: 'check',
        data:{
          passwd: this.data.passwd,
          openid: this.data._openid,
          rid: this.data.rid
        }
      }).then(res => {
        console.log(res)
        Toast.success('签到成功')
        this.onLoad()
      })
    }
  },

  passwdCheckChange: function(e){
    this.setData({
      passwd_check: e.detail
    })
  },

  openDialog: function(e){
    this.setData({
      show: true,
      _openid: e.target.dataset.openid,
      rid: e.target.dataset.rid,
      passwd: e.target.dataset.passwd
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }
    wx.cloud.callFunction({
      name: 'list_record'
    }).then(res => {
      this.setData({
        records: res.result
      })
      console.log(res.result)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTabBar().init();
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }
    wx.cloud.callFunction({
      name: 'list_record'
    }).then(res => {
      this.setData({
        records: res.result
      })
      console.log(res.result)
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})